﻿Namespace Base
    Public NotInheritable Class AppManager
        Private Shared _applicationOption As AppOption
        Private Shared _userID As Guid
        Private Shared _isConnected As Boolean = False
        Private Shared _clientSDK As String
        Private Shared _defaultSDK As String
        Private Shared _clientWinSDK As String
        Private Shared _defaultWinSDK As String
        'hack for now aleblanc / 2012-04-17
        Private Shared _metaApps As MetaApplication

#Region "app.config/web.config connection information is needed WEB SERVICE use it"
        Private Shared _connectionString As String
        Private Shared _connectionConfigName As String
        Private Shared _connectionStringName As String
        Private Shared _smtpServer As String
        Private Shared _supportFROM As String
        Private Shared _supportTO As String
        Private Shared _smtpPort As Integer? = Nothing

        Private Shared _ByPassConenctionString As Boolean
        Private Shared _OverrideByConnectionStringTOMaster As Boolean
        Private Shared _ByPassConnectionStringTOMaster As String = Nothing

        Private Shared _ExportToMasterDB As String = "Master"
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Property ByPassConnectionString() As Boolean
            Get
                Return _ByPassConenctionString
            End Get
            Set(ByVal value As Boolean)
                _ByPassConenctionString = value
            End Set
        End Property
        ''' <summary>
        ''' Property to get the connection name in the config file
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Property ByPassConnectionStringTOMaster() As String
            Get
                '_ByPassConnectionStringTOMaster = AppManager.SqlConnectionString(AppManager.DbServerName, AppManager.DbName, AppManager.UserName, AppManager.Password, AppManager.IsTrustedConnection)
                Return _ByPassConnectionStringTOMaster
            End Get
            Set(ByVal value As String)
                _ByPassConnectionStringTOMaster = value
            End Set
        End Property
        ''' <summary>
        ''' Override the default connection to use the ByPassConnectionStringTOMaster 
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Property OverrideByPassConnectionStringTOMaster() As Boolean
            Get
                '_ByPassConnectionStringTOMaster = AppManager.SqlConnectionString(AppManager.DbServerName, AppManager.DbName, AppManager.UserName, AppManager.Password, AppManager.IsTrustedConnection)
                Return _OverrideByConnectionStringTOMaster
            End Get
            Set(ByVal value As Boolean)
                _OverrideByConnectionStringTOMaster = value
            End Set
        End Property

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared ReadOnly Property ByPassConnectionStringDefault() As String
            Get
                ConnectionConfigName = "ConnectionToMyMaster"
                Return ConfigurationManager.ConnectionStrings(ConnectionStringName).ConnectionString
            End Get
        End Property

        ''' <summary>
        ''' Property to get the connectionstring in the config file
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Property ConnectionString() As String
            Get
                _connectionString = AppManager.SqlConnectionString(AppManager.DbServerName, AppManager.DbName, AppManager.UserName, AppManager.Password, AppManager.IsTrustedConnection)
                Return _connectionString
            End Get
            Set(ByVal value As String)
                _connectionString = value
            End Set
        End Property
        ''' <summary>
        ''' Property to get or set the ConnectionConfigName to use in App.Config
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Property ConnectionConfigName() As String
            Get
                Return _connectionConfigName
            End Get
            Set(value As String)
                _connectionConfigName = value
            End Set
        End Property
        ''' <summary>
        ''' Property to get the connection name in the config file
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Property ConnectionStringName() As String
            Get
                If String.IsNullOrEmpty(_connectionConfigName) Then
                    _connectionStringName = ConfigurationManager.AppSettings.Get("DefaultConnectionStringName")
                Else
                    'If ConnectionConfigName = "ConnectionToMyMaster" And _ByPassConnectionStringTOMaster Is Nothing Then
                    '    _ByPassConenctionString = False
                    'Else
                    '    _ByPassConenctionString = True
                    'End If
                    _connectionStringName = ConfigurationManager.AppSettings.Get(ConnectionConfigName)
                    ConnectionConfigName = Nothing
                End If
                Return _connectionStringName
            End Get
            Set(value As String)
                _connectionStringName = value
            End Set
        End Property
#End Region
        ''' <summary>
        ''' Name of the connected Database
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Property DbName() As String
        ''' <summary>
        ''' Name of the Connected Server
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Property DbServerName() As String
        ''' <summary>
        ''' Are we connected to the Database
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Property IsConnected() As Boolean
            Get
                Return _isConnected
            End Get
            Set(ByVal value As Boolean)
                _isConnected = value
            End Set
        End Property

        Public Shared ReadOnly Property ExportToMasterDB() As String
            Get
                Dim oldValueIsConnected As Boolean = IsConnected
                Dim oldvalueOverrideByPassConnectionStringTOMaster As Boolean = OverrideByPassConnectionStringTOMaster
                IsConnected = False
                OverrideByPassConnectionStringTOMaster = True
                Dim Cn As SqlConnection = New SqlConnection(SqlConnectionString("", "", "", "", False))
                IsConnected = oldValueIsConnected
                OverrideByPassConnectionStringTOMaster = oldvalueOverrideByPassConnectionStringTOMaster
                Dim DBname As String = Cn.Database
                Cn.Dispose()
                Return DBname
            End Get
        End Property
        ''' <summary>
        ''' Are we using the Trusted Conneection
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Property IsTrustedConnection() As Boolean
        ''' <summary>
        ''' User Password, when using SQL Authentication
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks>Nothing when Windows Authentication</remarks>
        Public Shared Property Password() As String


        ''' <summary>
        ''' Current Framework that the application use. Configure with the MetaApplicationOptions
        ''' </summary>
        ''' <value>PLSDK</value>
        ''' <returns></returns>
        ''' <remarks>ClientSDK is where we put customize PL logic for the client
        ''' The Key in appconfig file is CLIENTPLSDK and can b</remarks>
        Public Shared Property ClientSDK() As String
            Get
                If _clientSDK Is Nothing Then
                    _metaApps = New MetaApplication("SystemBOX")
                    If _metaApps.MetaApplicationOptions.ContainsKey("CLIENTPLSDK") Then
                        If Not String.IsNullOrWhiteSpace(_metaApps.MetaApplicationOptions("CLIENTPLSDK").DfltOptionValue) Then
                            _clientSDK = _metaApps.MetaApplicationOptions("CLIENTPLSDK").DfltOptionValue
                        End If
                    End If
                End If
                Return _clientSDK
            End Get
            Set(ByVal value As String)
                _clientSDK = value
            End Set
        End Property

        ''' <summary>
        ''' Current Framework that the application use. Configure with the MetaApplicationOptions
        ''' </summary>
        ''' <value>WinSDK</value>
        ''' <returns></returns>
        ''' <remarks>ClientWinSDK is where we put customize Winform behevior logic for the client
        ''' The Key in appconfig file is CLIENTWinSDK and can b</remarks>
        Public Shared Property ClientWinSDK() As String
            Get
                If _clientWinSDK Is Nothing Then
                    _metaApps = New MetaApplication("SystemBOX")
                    If _metaApps.MetaApplicationOptions.ContainsKey("CLIENTWinSDK") Then
                        If Not String.IsNullOrWhiteSpace(_metaApps.MetaApplicationOptions("CLIENTWinSDK").DfltOptionValue) Then
                            _clientWinSDK = _metaApps.MetaApplicationOptions("CLIENTWinSDK").DfltOptionValue
                        End If
                    End If
                End If
                Return _clientWinSDK
            End Get
            Set(ByVal value As String)
                _clientWinSDK = value
            End Set
        End Property

        Public Shared Property DefaultWinSDK() As String
            Get
                If _defaultWinSDK Is Nothing Then
                    _metaApps = New MetaApplication("SystemBOX")
                    If _metaApps.MetaApplicationOptions.ContainsKey("DEFAULTWinSDK") Then
                        If Not String.IsNullOrWhiteSpace(_metaApps.MetaApplicationOptions("DEFAULTWinSDK").DfltOptionValue) Then
                            _defaultWinSDK = _metaApps.MetaApplicationOptions("DEFAULTWinSDK").DfltOptionValue
                        End If
                    End If
                End If
                Return _defaultWinSDK
            End Get
            Set(ByVal value As String)
                _defaultWinSDK = value
            End Set
        End Property
        ''' <summary>
        ''' Current Framework that the application use. Configure with the MetaApplicationOptions
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Property DefaultSDK() As String
            Get
                If _defaultSDK Is Nothing Then
                    _metaApps = New MetaApplication("SystemBOX")
                    If _metaApps.MetaApplicationOptions.ContainsKey("DEFAULTPLSDK") Then
                        If Not String.IsNullOrWhiteSpace(_metaApps.MetaApplicationOptions("DEFAULTPLSDK").DfltOptionValue) Then
                            _defaultSDK = _metaApps.MetaApplicationOptions("DEFAULTPLSDK").DfltOptionValue
                        End If
                    End If
                End If
                Return _defaultSDK
            End Get
            Set(ByVal value As String)
                _defaultSDK = value
            End Set
        End Property
        ''' <summary>
        ''' SMTP server use to Send Email
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Property SMTPServer() As String
            Get
                If String.IsNullOrEmpty(_smtpServer) Then
                    _smtpServer = MetaValue.GetMetaValueByCode("OPT_SYSMAILSERVER").Value
                End If
                Return _smtpServer
            End Get
            Set(value As String)
                _smtpServer = value
            End Set
        End Property
        ''' <summary>
        ''' Support error From
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Property SupportFROM() As String
            Get
                If String.IsNullOrEmpty(_supportFROM) Then
                    _supportFROM = MetaValue.GetMetaValueByCode("OPT_SYSMAILSUPPORTFROM").Value
                End If
                Return _supportFROM
            End Get
            Set(value As String)
                _supportFROM = value
            End Set
        End Property
        ''' <summary>
        ''' Support Error To
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Property SupportTO() As String
            Get
                If String.IsNullOrEmpty(_supportTO) Then
                    _supportTO = MetaValue.GetMetaValueByCode("OPT_SYSMAILSUPPORTTO").Value
                End If
                Return _supportTO
            End Get
            Set(value As String)
                _supportTO = value
            End Set
        End Property
        ''' <summary>
        ''' SMTP port for email server
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks>Default Value = 25</remarks>
        Public Shared Property SMTPPort() As Integer?
            Get
                Dim val As String
                If _smtpPort Is Nothing Then
                    val = MetaValue.GetMetaValueByCode("OPT_SYSMAILSERVERPORT").Value
                    If Not String.IsNullOrEmpty(val) Then
                        _smtpPort = CInt(val)
                    Else
                        _smtpPort = 25
                    End If
                End If
                Return _smtpPort
            End Get
            Set(value As Integer?)
                _smtpPort = value
            End Set
        End Property
        ''' <summary>
        ''' UserId from the MetaUser
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks>UserID is Match with the UserName</remarks>
        Public Shared Property UserID() As Guid
            Get
                If _userID = Guid.Empty Then
                    _userID = GetUserID(UserName)
                End If
                Return _userID
            End Get
            Set(ByVal value As Guid)
                _userID = value
            End Set
        End Property
        ''' <summary>
        ''' UserName of the Connected User
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Property UserName() As String

        'OutStandingIssue: Aller cherher de la table Employe - aleblanc - 2012-12-19
        'Modifier GetUserID(UserName) pour retourner les 2 colonnes le ID et le Email.
        Public Shared Property Email() As String

        Public Shared Property SiteAccessList As String
        Public Shared Property SocietyAccessList As String
        Public Shared Property DataSubjectAccess As DataTable
        Public Shared Property DataFieldAccess As DataTable
        Public Shared Property GroupListAccess As DataTable
        Public Shared Property MetaColumnAccess As DataTable

        Public Shared Property ApplicationOption() As AppOption
            Get
                'If _applicationOption Is Nothing Then
                _applicationOption = New AppOption()
                'End If
                Return _applicationOption
            End Get
            Set(ByVal value As AppOption)
                _applicationOption = value
            End Set
        End Property

        ''' <summary>
        ''' Language de l'application du user 
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks>
        ''' HACK: Afin de setter le language ID Global quand on le sais
        ''' Voir AppOption.LaunguageId comment on l'assigne
        ''' </remarks>
        Public Shared Property LanguageId As Guid = System.Guid.Empty

        Public Shared Sub ClearContext()
            _clientSDK = Nothing
            _defaultSDK = Nothing
            _applicationOption = Nothing
            _connectionString = Nothing
            _userID = Nothing
            UserName = Nothing
            Password = Nothing
            DbServerName = Nothing
            DbName = Nothing
            IsTrustedConnection = Nothing
            _connectionString = Nothing
            _connectionStringName = Nothing
            _isConnected = False
            '
            If DataSubjectAccess IsNot Nothing Then DataSubjectAccess.Dispose()
            If DataFieldAccess IsNot Nothing Then DataFieldAccess.Dispose()
            If MetaColumnAccess IsNot Nothing Then MetaColumnAccess.Dispose()
            '
            DataSubjectAccess = Nothing
            DataFieldAccess = Nothing
            MetaColumnAccess = Nothing
            SocietyAccessList = Nothing
            SiteAccessList = Nothing
        End Sub

        Public Shared Sub ResetCache()
            ApplicationOption = Nothing
            Cache.CacheExt.ClearCache()
            'OutStandingIssue: Ne pas oublier les fichiers encrypter, quand ils seront prêt... aleblanc - 2011-06-29
        End Sub
        ''' <summary>
        ''' Return a valid Database Connection String 
        ''' If the users IsConnected form the LoginWindow We use is account
        ''' If not Loggin we use the connection string from the app.config or Web. Config file
        ''' </summary>
        ''' <param name="serverName">Server To Connect to</param>
        ''' <param name="database">Database To Connect to</param>
        ''' <param name="userName">Username Loggin</param>
        ''' <param name="password">User Password</param>
        ''' <param name="trusted">Do we use Window Authentication</param>
        ''' <returns></returns>
        ''' <remarks>MARS and Pooling are activated, MinPoolSize=0, MaxPoolSize=0, MinPoolSize=0, LoadBalanceTimeout=1</remarks>
        Public Shared Function SqlConnectionString(ByVal serverName As String, ByVal database As String, ByVal userName As String, ByVal password As String, ByVal trusted As Boolean) As String
            Dim cnstr As SqlConnectionStringBuilder = New System.Data.SqlClient.SqlConnectionStringBuilder()

            'Check if we have a connection from config file
            If AppManager.IsConnected = False Then

                If Not String.IsNullOrEmpty(ConnectionStringName) Then
                    If _ByPassConenctionString = False Then
                        If _OverrideByConnectionStringTOMaster Then
                            _connectionString = ByPassConnectionStringDefault
                        Else
                            _connectionString = ConfigurationManager.ConnectionStrings(ConnectionStringName).ConnectionString
                        End If
                    Else
                        _connectionString = _ByPassConnectionStringTOMaster
                    End If
                    cnstr.ConnectionString = _connectionString
                End If
            Else
                'If it's TRUE User has loggin with the LoginWindow
                cnstr = New System.Data.SqlClient.SqlConnectionStringBuilder()
                cnstr.DataSource = serverName
                cnstr.InitialCatalog = database
                cnstr.IntegratedSecurity = trusted
                cnstr.MultipleActiveResultSets = True 'Le default est False
                cnstr.Pooling = True     ' le defaut    
                cnstr.MinPoolSize = 0    ' le defaut 
                cnstr.MaxPoolSize = 200   ' le defaut est 100
                cnstr.LoadBalanceTimeout = 1 ' 5 secondes avant de l'enlever, si pu utiliser - En test 2011-09-06  / le défaut est 0
                cnstr.UserID = userName
                cnstr.ApplicationName = "SystemBOX"
                If Not trusted Then
                    cnstr.Password = password
                End If
            End If
            Return cnstr.ConnectionString
        End Function


        ''' <summary>
        ''' Go get the userID from the UserName
        ''' </summary>
        ''' <param name="userName"></param>
        ''' <returns></returns>
        ''' <remarks>Use to get the comlumn base on security profile</remarks>
        Public Shared Function GetUserID(ByVal userName As String) As Guid
            Using aMq As MetaQuery = MetaQuery.GetMetaQueryByName("MetaUsers")
                If aMq IsNot Nothing Then
                    aMq.SQLWhere = String.Format("UserName = '{0}'", userName)
                    aMq.FillResultQuery()
                    If aMq.ResultQuery.Rows.Count > 0 Then
                        Return New System.Guid(aMq.ResultQuery.Rows(0).Item("UserID").ToString)
                    End If
                End If
            End Using
            Return Nothing
        End Function


        ''' <summary>
        ''' From user config, we load the columns he have writes on.
        ''' </summary>
        ''' <param name="queryName"></param>
        ''' <param name="userID"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function GetUserColumnSecurity(ByVal queryName As String, ByVal userID As Guid) As DataTable
            Dim aArrayOfDataRow As DataRow()
            Dim filter As String = String.Format("QueryName = '{0}'", queryName)
            aArrayOfDataRow = AppManager.MetaColumnAccess.Select(filter)
            Return aArrayOfDataRow.CopyToDataTable()
        End Function


        ''' <summary>
        ''' Get the list of GCCTS of the users.
        ''' If no security set the list return is Guid.Empty()
        ''' </summary>
        ''' <param name="accessType"></param>
        ''' <param name="listId"></param>
        ''' <returns>Une liste de guid de type string ex: 'GUID', 'GUID' </returns>
        ''' <remarks>Le Guid Empty est retourné pour les cas ou on utilise la liste dans une requete SQL.</remarks>
        Public Shared Function GetGCCTSSecurity(ByVal accessType As String, ByVal listId As Guid) As String
            Dim aDataFieldSecurity As DataView = (From oDataFieldSecurity In DataFieldAccess.AsEnumerable() _
                 Where oDataFieldSecurity.Field(Of String)("Type") = accessType _
                   And oDataFieldSecurity.Field(Of Guid)("ListID") = listId _
                 Select oDataFieldSecurity).AsDataView
            If aDataFieldSecurity.Count = 0 OrElse String.IsNullOrWhiteSpace(aDataFieldSecurity.Item(0).Row("InValue").ToString) OrElse aDataFieldSecurity.Item(0).Row("InValue").ToString = "''" Then
                Return String.Format("'{0}'", Guid.Empty)
            End If
            Return (aDataFieldSecurity.Item(0).Row("InValue").ToString)
        End Function


        ''' <summary>
        ''' Get the list of GCCTS of the users.
        ''' If no security set the list return is Guid.Empty()
        ''' </summary>
        ''' <param name="accessType"></param>
        ''' <param name="listId"></param>
        ''' <returns>Une liste de guid de type string ex: 'GUID', 'GUID' </returns>
        ''' <remarks>Le Guid Empty est retourné pour les cas ou on utilise la liste dans une requete SQL.</remarks>
        Public Shared Function GetGCCTSSecurity(ByVal accessType As String, ByVal listId As Guid, ByRef empty As Boolean) As String
            Dim aDataFieldSecurity As DataView = (From oDataFieldSecurity In DataFieldAccess.AsEnumerable() _
                 Where oDataFieldSecurity.Field(Of String)("Type") = accessType _
                   And oDataFieldSecurity.Field(Of Guid)("ListID") = listId _
                 Select oDataFieldSecurity).AsDataView
            If aDataFieldSecurity.Count = 0 OrElse String.IsNullOrWhiteSpace(aDataFieldSecurity.Item(0).Row("InValue").ToString) OrElse aDataFieldSecurity.Item(0).Row("InValue").ToString = "''" Then
                empty = True
                Return String.Format("'{0}'", Guid.Empty)
            End If
            empty = False
            Return (aDataFieldSecurity.Item(0).Row("InValue").ToString)
        End Function


        ''' <summary>
        '''  This function will return TRUE if current user is in SystemBOXAdmin Group security or in Power User group
        ''' </summary>
        ''' <returns>Boolean</returns>
        ''' <remarks></remarks>
        Public Shared Function IsPowerUser() As Boolean
            Dim agrouplist As DataView
            If GroupListAccess Is Nothing Then Return Nothing
            agrouplist = (From oDataSubjectAccess In GroupListAccess.AsEnumerable() _
                    Where oDataSubjectAccess.Field(Of String)("UserName") = UserName _
                      And (oDataSubjectAccess.Field(Of Guid)("GroupID_FK") = IdRepository.META_METAUSERGROUP_SYSTEMBOXADMIN Or oDataSubjectAccess.Field(Of Guid)("GroupID_FK") = IdRepository.META_METAUSERGROUP_POWERUSER) _
                    Select oDataSubjectAccess).AsDataView
            If agrouplist.Count = 0 Then
                Return False
            Else
                Return True
            End If
        End Function

        ''' <summary>
        ''' This function will return TRUE if current user is in SystemBOXAdmin Group security
        ''' </summary>
        ''' <returns>Boolean</returns>
        ''' <remarks></remarks>
        Public Shared Function IsSysBOXAdmin() As Boolean
            Dim agrouplist As DataView
            If GroupListAccess Is Nothing Then Return Nothing
            agrouplist = (From oDataSubjectAccess In GroupListAccess.AsEnumerable() _
                    Where oDataSubjectAccess.Field(Of String)("UserName") = UserName _
                      And (oDataSubjectAccess.Field(Of Guid)("GroupID_FK") = IdRepository.META_METAUSERGROUP_SYSTEMBOXADMIN) _
                    Select oDataSubjectAccess).AsDataView
            If agrouplist.Count = 0 Then
                Return False
            Else
                Return True
            End If
            Return False
        End Function


        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="controlCD"></param>
        ''' <param name="controlType"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function GetControlSecurity(ByVal controlCD As String, ByVal controlType As String) As DataView
            Dim aDataSubjectSecurity As DataView
            If DataSubjectAccess Is Nothing Then Return Nothing
            aDataSubjectSecurity = (From oDataSubjectAccess In DataSubjectAccess.AsEnumerable() _
                    Where oDataSubjectAccess.Field(Of String)("ControlCd") = controlCD _
                      And oDataSubjectAccess.Field(Of String)("ControlType") = controlType _
                    Select oDataSubjectAccess).AsDataView
            If aDataSubjectSecurity.Count = 0 Then
                Return Nothing
            End If
            Return aDataSubjectSecurity
        End Function

        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="formCD"></param>
        ''' <param name="queryName"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function GetControlMqFromForm(ByVal formCD As String, ByVal queryName As String) As String
            Dim aDataSubjectSecurity As DataView
            If DataSubjectAccess Is Nothing Then Return Nothing
            aDataSubjectSecurity = (From oDataSubjectAccess In DataSubjectAccess.AsEnumerable() _
                    Where oDataSubjectAccess.Field(Of String)("LogicalGroup") Like "*" + formCD + "*" _
                      And oDataSubjectAccess.Field(Of String)("ControlType") = "METAQ" _
                      And oDataSubjectAccess.Field(Of String)("ControlName") = queryName _
                   Select oDataSubjectAccess).AsDataView
            If aDataSubjectSecurity.Count = 0 Then
                Return Nothing
            End If
            Return aDataSubjectSecurity.Item(0).Row("ControlCD").ToString()
        End Function
    End Class
End Namespace
